#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int mp[100005] = { 0 };
        size_t left = 0, max = 0, j = 0;
        for (size_t i = 0; i < fruits.size(); i++)
        {
            if (++mp[fruits[i]] == 1) j++;
            while (j >= 3) if (--mp[fruits[left++]] == 0) j--;
            if (i - left + 1 > max) max = i - left + 1;
        }
        return max;
    }
};